package com.example.jdbcdemo.service.Impl;

import com.alibaba.fastjson.JSON;
import com.example.jdbcdemo.mapper.GoodsMapper;
import com.example.jdbcdemo.mapper.ProductMapper;
import com.example.jdbcdemo.mapper.StorageMapper;
import com.example.jdbcdemo.pojo.Goods;
import com.example.jdbcdemo.pojo.Product;
import com.example.jdbcdemo.service.GoodsServiceInterface;
import com.example.jdbcdemo.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsServiceInterface {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    StorageMapper storageMapper;

    @Override
    public JsonUtil addGoods(int productId, int num, int storageId) {
        goodsMapper.addGoods(new Goods(0,productId,storageId,num));
        return new JsonUtil(0,"添加新库存成功","");
    }

    @Override
    public JsonUtil deleteGoods(int id) {
        goodsMapper.deleteGoods(id);
        return new JsonUtil(0,"删除该库存成功","");
    }

    @Override
    public JsonUtil queryGoodsList() {
        List<Goods> goodsList=goodsMapper.queryGoodsList();
        List<Map<String,Object>> mapMap=new LinkedList<>();
        for (Goods goods:goodsList){
            Map<String,Object> map=new LinkedHashMap<>();
            Product product=productMapper.queryProductById(goods.getProductId());
            map.put("goodsId",goods.getId());
            map.put("storageId",goods.getStorageId());
            map.put("storageName",storageMapper.queryStorageById(goods.getStorageId()).getAddress());
            map.put("productId",product.getId());
            map.put("name",product.getName());
            map.put("num",goods.getNumber());
            map.put("cost",product.getCost());
            map.put("wholesale",product.getWholeSale());
            map.put("singlesale",product.getSingleSale());
            mapMap.add(map);
        }
        return new JsonUtil(0,"查询成功", JSON.toJSONString(mapMap));
    }

    @Override
    public JsonUtil updateGoods(int id, int productId, int num, int storageId) {
        goodsMapper.updateGoods(new Goods(id,productId,storageId,num));
        return new JsonUtil(0,"更新成功","");
    }

    @Override
    public JsonUtil queryGoodsById(int id) {
        Goods goods=goodsMapper.queryGoodsById(id);
        Map<String,Object> map=new LinkedHashMap<>();
        Product product=productMapper.queryProductById(goods.getProductId());
        map.put("goodsId",goods.getId());
        map.put("productId", goods.getProductId());
        map.put("storageId",goods.getStorageId());
        map.put("storageName",storageMapper.queryStorageById(goods.getStorageId()).getAddress());
        map.put("productId",product.getId());
        map.put("name",product.getName());
        map.put("num",goods.getNumber());
        map.put("cost",product.getCost());
        map.put("wholesale",product.getWholeSale());
        map.put("singlesale",product.getSingleSale());
        return new JsonUtil(0,"根据Id查询成功",JSON.toJSONString(map));
    }

    /**
     * 调拨货物
     * @param goodsId 货物（库存）id
     * @param targetId 目标仓库的id
     * @param num  要调拨的数量
     * @return
     */
    @Override
    public JsonUtil changeGoodsInStorage(int goodsId, int targetId, int num) {
        if (num <= 0) {
            return new JsonUtil(501,"数量不能小于等于0","");
        }
        Goods goods=goodsMapper.queryGoodsById(goodsId);
        int productid=goods.getProductId();//获取商品id
        int oldnum=goods.getNumber();//获取商品数量
        if (oldnum<num){
            return new JsonUtil(501,"数量不够","");
        }
        int storageid=goods.getStorageId();
        goodsMapper.updateGoods(new Goods(goodsId,productid,storageid,oldnum-num));//将货物移出旧仓库
        Goods newGoods=goodsMapper.queryGoodsByTwo(productid,targetId);
        if (newGoods==null){
            goodsMapper.addGoods(new Goods(0,productid,targetId,num));
        }
        else
        {
            int id=newGoods.getId();
            goodsMapper.updateGoods(new Goods(id,productid,targetId, newGoods.getNumber()+num));
        }
        return new JsonUtil(0,"移动成功","");
    }


}
